package com.xunjie.check;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class DataBaseCheck implements ApplicationRunner {
    private static Logger log = LoggerFactory.getLogger(DataBaseCheck.class);

    private final JdbcTemplate jdbcTemplate;

    public DataBaseCheck(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        String sql = "select 1";
        try {
            jdbcTemplate.queryForList(sql);
        }catch (Exception e){
            log.error("【数据库连接失败!】{}",e.getMessage());
            throw new RuntimeException(e);
        }
        log.info("【数据库连接成功!】");

    }
}
